home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / tee.c < prev    next >
C/C++ Source or Header  |  1992-04-29  |  4KB  |  156 lines

  1. /* tee - read from standard input and write to standard output and files.
  2.    Copyright (C) 1985, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Mike Parker, Richard M. Stallman, and David MacKenzie */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <signal.h>
  23. #include <getopt.h>
  24. #include "system.h"
  25.  
  26. char *xmalloc ();
  27. int tee ();
  28. void error ();
  29. void xwrite ();
  30.  
  31. /* If nonzero, append to output files rather than truncating them. */
  32. int append;
  33.  
  34. /* If nonzero, ignore interrupts. */
  35. int ignore_interrupts;
  36.  
  37. /* The name that this program was run with. */
  38. char *program_name;
  39.  
  40. struct option long_options[] =
  41. {
  42.   {"append", 0, NULL, 'a'},
  43.   {"ignore-interrupts", 0, NULL, 'i'},
  44.   {NULL, 0, NULL, 0}
  45. };
  46.  
  47. void
  48. main (argc, argv)
  49.      int argc;
  50.      char **argv;
  51. {
  52.   int errs;
  53.   int optc;
  54.     
  55.   program_name = argv[0];
  56.   append = 0;
  57.   ignore_interrupts = 0;
  58.  
  59.   while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
  60.      != EOF)
  61.     {
  62.       switch (optc)
  63.     {
  64.     case 'a':
  65.       append = 1;
  66.       break;
  67.     case 'i':
  68.       ignore_interrupts = 1;
  69.       break;
  70.     default:
  71.       fprintf (stderr, "\
  72. Usage: %s [-ai] [--append] [--ignore-interrupts] [file...]\n",
  73.            program_name);
  74.       exit (1);
  75.     }
  76.     }
  77.  
  78.   if (ignore_interrupts)
  79. #ifdef _POSIX_VERSION
  80.     {
  81.       struct sigaction sigact;
  82.  
  83.       sigact.sa_handler = SIG_IGN;
  84.       sigemptyset (&sigact.sa_mask);
  85.       sigact.sa_flags = 0;
  86.       sigaction (SIGINT, &sigact, NULL);
  87.     }
  88. #else                /* !_POSIX_VERSION */
  89.     signal (SIGINT, SIG_IGN);
  90. #endif                /* _POSIX_VERSION */
  91.  
  92.   errs = tee (argc - optind, &argv[optind]);
  93.   if (close (0) == -1)
  94.     error (1, errno, "standard input");
  95.   if (close (1) == -1)
  96.     error (1, errno, "standard output");
  97.   exit (errs);
  98. }
  99.  
  100. /* Copy the standard input into each of the NFILES files in FILES
  101.    and into the standard output.
  102.    Return 0 if successful, 1 if any errors occur. */
  103.  
  104. int
  105. tee (nfiles, files)
  106.      int nfiles;
  107.      char **files;
  108. {
  109.   int *descriptors;
  110.   char buffer[BUFSIZ];
  111.   register int bytes_read, i, ret = 0, mode;
  112.  
  113.   if (nfiles)
  114.     descriptors = (int *) xmalloc (nfiles * sizeof (int));
  115.   mode = O_WRONLY | O_CREAT;
  116.   if (append)
  117.     mode |= O_APPEND;
  118.   else
  119.     mode |= O_TRUNC;
  120.  
  121.   for (i = 0; i < nfiles; i++)
  122.     {
  123.       descriptors[i] = open (files[i], mode, 0666);
  124.       if (descriptors[i] == -1)
  125.     {
  126.       error (0, errno, "%s", files[i]);
  127.       ret = 1;
  128.     }
  129.     }
  130.  
  131.   while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
  132.     {
  133.       xwrite (1, buffer, bytes_read);
  134.       for (i = 0; i < nfiles; i++)
  135.     if (descriptors[i] != -1)
  136.       xwrite (descriptors[i], buffer, bytes_read);
  137.     }
  138.   if (bytes_read == -1)
  139.     {
  140.       error (0, errno, "read error");
  141.       ret = 1;
  142.     }
  143.  
  144.   for (i = 0; i < nfiles; i++)
  145.     if (descriptors[i] != -1 && close (descriptors[i]) == -1)
  146.       {
  147.     error (0, errno, "%s", files[i]);
  148.     ret = 1;
  149.       }
  150.  
  151.   if (nfiles)
  152.     free (descriptors);
  153.  
  154.   return ret;
  155. }
  156.